/*
 * @Description: 动态网页标题
 * @Author: ZhangHan
 * @Date: 2025-05-28 11:33:24
 * @LastEditTime: 2025-05-28 11:44:45
 * @LastEditors: ZhangHan
 */
import { useEffect } from "react";
import { useLocation } from "react-router-dom";
import { useMenu } from "@/utils/useMenu";

function usePageTitle() {
  const location = useLocation();
  //数组扁平化
  const menus = useMenu().flatMap((item) => {
    return item?.children;
  });

  useEffect(() => {
    const pageTitle = menus.find(
      (item) => item.path === location.pathname
    )?.title;
    document.title = pageTitle
      ? `${pageTitle} - 豌豆一点都不老`
      : `豌豆一点都不老`;
  }, [location, menus]);
}

export default usePageTitle;
